using System;
using System.Collections;
using BitMango;
using BitMango.Diagnostics;
using UnityEngine;

[ExecuteInEditMode]
public class PlatformContext : MonoBehaviour
{
    private void Awake()
    {
        Context.BMPlatform = this;
        if (!Platform.IsEU() || (Platform.IsEU() && Platform.IsTOSAgreed))
        {
        }
        this.isFirstRun = !PlayerPrefs.HasKey("lastPlayed");
        if (PlayerPrefs.HasKey("lastPlayed"))
        {
            this.lastPlayed = DateTime.Parse(PlayerPrefs.GetString("lastPlayed"));
        }
        else
        {
            this.lastPlayed = DateTime.UtcNow;
        }
        if (!PlayerPrefs.HasKey("installed"))
        {
            this.installed = DateTime.UtcNow;
            PlayerPrefs.SetString("installed", this.installed.ToString());
        }
        else
        {
            this.installed = DateTime.Parse(PlayerPrefs.GetString("installed"));
        }
        UnityEngine.Debug.Log("Initialize Debugger");
        string text = string.Empty;
        string text2 = string.Empty;
        UnityEngine.Debug.Log("Debugger.EnableLocalLog:" + Debugger.EnableLocalLog);
        if (Debugger.EnableLocalLog)
        {
            text = "\n\tADID: " + Profile.ADID;
        }
        text2 = string.Concat(new object[]
        {
            "\n\tScreen - Width: ",
            Screen.width,
            ", Height: ",
            Screen.height
        });
        string empty = string.Empty;
        if (Profile.IsEchoInput)
        {
            Debugger.Log("IsEchoInput");
            base.StartCoroutine(this.coEchoInput());
        }
        base.StartCoroutine(this.coRecordFPS());
    }

    private void OnApplicationPause(bool pause)
    {
        Debugger.Log(string.Concat(new object[]
        {
            "OnApplicationPause: ",
            pause,
            " recoverPoint: ",
            this.recoverPoint
        }));
        if (pause)
        {
            PlayerPrefs.SetString("lastPlayed", DateTime.UtcNow.ToString());
            if (this.recoverPoint == null)
            {
                base.BroadcastMessage("OnGamePause", pause, SendMessageOptions.DontRequireReceiver);
            }
        }
        else
        {
            if (this.recoverPoint == null)
            {
                if (Context.AdManager != null && !Context.AdManager.VideoAdShown)
                {
                    Context.AdManager.ShowInterstitial("resume", true, null);
                }
                Context.AdManager.VideoAdShown = false;
                base.BroadcastMessage("OnGamePause", pause, SendMessageOptions.DontRequireReceiver);
                ControlCenter.GetControlCenterInfo();
            }
            this.recoverPoint = null;
        }
    }

    private void OnApplicationQuit()
    {
        Debugger.Log("OnApplicationQuit : recoverPoint:" + this.recoverPoint);
        PlayerPrefs.SetString("lastPlayed", DateTime.UtcNow.ToString());
    }

    private void OnGUI()
    {
    }

    private IEnumerator coEchoInput()
    {
        for (; ; )
        {
            if (UnityEngine.Input.touchCount > 0)
            {
                Touch touch = Input.touches[0];
                float x = touch.position.x;
                float y = touch.position.y;
                Debugger.Log(string.Concat(new object[]
                {
                    "EI] ",
                    touch.phase,
                    " (",
                    x,
                    ",",
                    y,
                    ")"
                }));
            }
            yield return null;
        }
        yield break;
    }

    private IEnumerator coRecordFPS()
    {
        float frequencySec = 2f;
        for (; ; )
        {
            int lastFrameCount = Time.frameCount;
            float lastTime = Time.realtimeSinceStartup;
            yield return new WaitForSeconds(frequencySec);
            float timeSpan = Time.realtimeSinceStartup - lastTime;
            int frameCount = Time.frameCount - lastFrameCount;
            float fps = (float)Mathf.RoundToInt((float)frameCount / timeSpan);
            Profile.UpdateFPSHistory(fps);
        }
        yield break;
    }

    [HideInInspector]
    public string pubID;

    public string appID;

    public string appDisplayName;

    private Vector3 prePosition;

    [HideInInspector]
    public string bundleVersion;

    [HideInInspector]
    public string bundleIdentifier;

    [HideInInspector]
    public string revision;

    [HideInInspector]
    public string sharedRevision;

    public PlatformContext.StoreType storeType;

    public string storeKey;

    [NonSerialized]
    public bool isFirstRun;

    [NonSerialized]
    public string recoverPoint;

    public DateTime lastPlayed;

    public DateTime installed;

    public enum StoreType
    {
        AppleStore,
        GoogleStore,
        Tstore,
        OllehStore,
        AmazonStore,
        BlackBerryStore,
        MacAppStore,
        MetroStore,
        WP8Store,
        FacebookStore
    }
}
